/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-showroom
*
* @File name : ShowRoomService.java
*
* @Author : Administrator
*
* @Date : 2017年11月13日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年11月13日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.service.showroom.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.marketing.common.exception.ServiceBizException;
import org.marketing.common.util.CheckUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.gmmc.common.service.BaseService;
import com.yonyou.gmmc.service.showroom.dao.TmModelMapper;
import com.yonyou.gmmc.service.showroom.model.TmModel;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @author Administrator TODO description
 * @date 2017年11月13日
 */
@Service
public class ShowRoomService extends BaseService {

	private static Logger logger = LoggerFactory.getLogger(ShowRoomService.class);

	@Autowired
	private TmModelMapper tmModelMapper;

	/**
	 * @author Lyq 获取品牌车系展示数据
	 * @date 2018年1月26日
	 * @return
	 */
	public List<Map<String, Object>> getBrandExhibitionData(String brandCode, String seriesCode) {
		logger.info("入参--brandCode:" + brandCode + "  seriesCode:" + seriesCode);
		List<Map<String, Object>> dtoList = new ArrayList<>();
		TmModel mo = new TmModel();
		mo.setDeleteFlag("1");
		if (!CheckUtil.NullOrEmpty(brandCode)) {
			mo.setBrandCode(brandCode);
		}
		if (!CheckUtil.NullOrEmpty(seriesCode)) {
			mo.setSeriesCode(seriesCode);
		}
		List<TmModel> list = new ArrayList<>();
		list = tmModelMapper.select(mo);
		// 对品牌数据进行分组处理
		dealModel(dtoList, list);
		return dtoList;
	}

	private void addSeries(List<Map<String, Object>> serList, TmModel tmModel) {
		Map<String, Object> serMap = new HashMap<>();
		serMap.put("seriesCode", tmModel.getSeriesCode());
		serMap.put("seriesName", tmModel.getSeriesName());
		serMap.put("picUrl", tmModel.getPicUrl());
		serMap.put("maxPrice", tmModel.getLocalPrice());
		serMap.put("minPrice", tmModel.getLocalPrice());
		List<String> displacementList = new ArrayList<>();
		if (!CheckUtil.NullOrEmpty(tmModel.getDisplacement())) {
			displacementList.add(tmModel.getDisplacement());
		}
		serMap.put("displacementList", displacementList);
		List<String> gearboxTypeList = new ArrayList<>();
		if (!CheckUtil.NullOrEmpty(tmModel.getGearboxType())) {
			gearboxTypeList.add(tmModel.getGearboxType());
		}
		serMap.put("gearboxTypeList", gearboxTypeList);
		List<Map<String, Object>> carList = new ArrayList<>();
		Map<String, Object> carMap = new HashMap<>();
		carMap.put("carCode", tmModel.getCarCode());
		carMap.put("carName", tmModel.getCarName());
		carMap.put("picUrl", tmModel.getPicUrl());
		carMap.put("guidePrice", tmModel.getGuidePrice());
		carMap.put("localPrice", tmModel.getLocalPrice());
		carMap.put("showName", tmModel.getBrandCode() + " " + tmModel.getSeriesCode() + " " + tmModel.getCarCode());
		carList.add(carMap);
		serMap.put("carList", carList);
		serList.add(serMap);
	}

	// 获取相同车型时的状态
	private boolean getFlag(List<Map<String, Object>> list, String str1, String str2) {
		boolean flag = false;
		for (int i = 0; i < list.size(); i++) {
			if (str1.equals(list.get(i).get(str2).toString())) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	// 获取相同车型时的状态
	private void getFlag(List<String> list, String str1) {
		boolean flag = false;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).equals(str1)) {
				flag = true;
				break;
			}
		}
		if (!flag) {
			list.add(str1);
		}
	}

	// 获取相同车型时的在List中的位置
	private int getNum(List<Map<String, Object>> list, String str1, String str2) {
		int g = 0;
		for (int i = 0; i < list.size(); i++) {
			if (str1.equals(list.get(i).get(str2).toString())) {
				g = i;
				break;
			}
		}
		return g;
	}

	/**
	 * 对品牌数据进行分组处理
	 * @param dtoList  分组后返回的集合
	 * @param list 分组前的集合
	 */
	@SuppressWarnings("unchecked")
	private void dealModel(List<Map<String, Object>> dtoList, List<TmModel> list) {
		for (TmModel tmModel : list) {
			if (!CheckUtil.NullOrEmpty(dtoList)) {
				boolean flag = getFlag(dtoList, tmModel.getBrandCode(), "brandCode");
				int g = getNum(dtoList, tmModel.getBrandCode(), "brandCode");

				if (flag) {
					List<Map<String, Object>> serList = (List<Map<String, Object>>) dtoList.get(g).get("serList");
					boolean flag1 = getFlag(serList, tmModel.getSeriesCode(), "seriesCode");
					int g1 = getNum(serList, tmModel.getSeriesCode(), "seriesCode");
					if (flag1) {
						if (!CheckUtil.NullOrEmpty(serList.get(g1).get("displacementList"))
								&& !CheckUtil.NullOrEmpty(tmModel.getDisplacement())) {
							getFlag((List<String>) serList.get(g1).get("displacementList"), tmModel.getDisplacement());
						}
						if (!CheckUtil.NullOrEmpty(serList.get(g1).get("gearboxTypeList"))
								&& !CheckUtil.NullOrEmpty(tmModel.getGearboxType())) {
							getFlag((List<String>) serList.get(g1).get("gearboxTypeList"), tmModel.getGearboxType());
						}
						if (!CheckUtil.NullOrEmpty(serList.get(g1).get("maxPrice"))
								&& !CheckUtil.NullOrEmpty(tmModel.getLocalPrice())) {
							BigDecimal maxPrice = (BigDecimal) serList.get(g1).get("maxPrice");
							serList.get(g1).put("maxPrice", maxPrice.max(tmModel.getLocalPrice()));
						} else if (CheckUtil.NullOrEmpty(serList.get(g1).get("maxPrice"))
								&& !CheckUtil.NullOrEmpty(tmModel.getLocalPrice())) {
							serList.get(g1).put("maxPrice", serList.get(g1).get("maxPrice"));
						} else if (!CheckUtil.NullOrEmpty(serList.get(g1).get("maxPrice"))
								&& CheckUtil.NullOrEmpty(tmModel.getLocalPrice())) {
							serList.get(g1).put("maxPrice", tmModel.getLocalPrice());
						}
						if (!CheckUtil.NullOrEmpty(serList.get(g1).get("minPrice"))
								&& !CheckUtil.NullOrEmpty(tmModel.getLocalPrice())) {
							BigDecimal maxPrice = (BigDecimal) serList.get(g1).get("minPrice");
							serList.get(g1).put("minPrice", maxPrice.max(tmModel.getLocalPrice()));
						} else if (CheckUtil.NullOrEmpty(serList.get(g1).get("minPrice"))
								&& !CheckUtil.NullOrEmpty(tmModel.getLocalPrice())) {
							serList.get(g1).put("minPrice", serList.get(g1).get("minPrice"));
						} else if (!CheckUtil.NullOrEmpty(serList.get(g1).get("minPrice"))
								&& CheckUtil.NullOrEmpty(tmModel.getLocalPrice())) {
							serList.get(g1).put("minPrice", tmModel.getLocalPrice());
						}
						List<Map<String, Object>> carList = (List<Map<String, Object>>) serList.get(g1).get("carList");
						boolean flag2 = getFlag(carList, tmModel.getCarCode(), "carCode");
						if (!flag2) {
							Map<String, Object> carMap = new HashMap<>();
							carMap.put("carCode", tmModel.getCarCode());
							carMap.put("carName", tmModel.getCarName());
							carMap.put("picUrl", tmModel.getPicUrl());
							carMap.put("guidePrice", tmModel.getGuidePrice());
							carMap.put("localPrice", tmModel.getLocalPrice());
							carMap.put("showName", tmModel.getBrandCode() + " " + tmModel.getSeriesCode() + " "
									+ tmModel.getCarCode());
							carList.add(carMap);
						}
					} else {
						addSeries(serList, tmModel);
					}
					dtoList.get(g).put("serList", serList);
				} else {
					Map<String, Object> brMap = new HashMap<>();
					brMap.put("brandCode", tmModel.getBrandCode());
					brMap.put("brandName", tmModel.getBrandName());
					List<Map<String, Object>> serList = new ArrayList<>();
					addSeries(serList, tmModel);
					brMap.put("serList", serList);
					dtoList.add(brMap);
				}
			} else {
				Map<String, Object> brMap = new HashMap<>();
				brMap.put("brandCode", tmModel.getBrandCode());
				brMap.put("brandName", tmModel.getBrandName());
				List<Map<String, Object>> serList = new ArrayList<>();
				addSeries(serList, tmModel);
				brMap.put("serList", serList);
				dtoList.add(brMap);
			}
		}
		// 计算可售数量
		for (Map<String, Object> map : dtoList) {
			List<Map<String, Object>> serList = (List<Map<String, Object>>) map.get("serList");
			int serNum = 0;
			for (Map<String, Object> map2 : serList) {
				List<Map<String, Object>> carList = (List<Map<String, Object>>) map2.get("carList");
				map2.put("seriesNum", carList.size());
				serNum += carList.size();
			}
			map.put("brandNum", serNum);
		}
	}

	/**
	 *
	 * @author lyq 获取车型展示数据
	 * @date 2018年1月26日
	 * @param seriesCode
	 * @return
	 */
	public TmModel getSeriesExhibitionData(String packageCode) {
		if (CheckUtil.NullOrEmpty(packageCode)) {
			logger.warn("getModelExhibitionData-packageCode is null");
			throw new ServiceBizException("packageCode is null");
		}
		Example ex = new Example(TmModel.class);
		ex.createCriteria().andEqualTo("deleteFlag", "1").andEqualTo("carCode", packageCode);
		ex.setOrderByClause("MODEL_CODE_NEW ASC");
		List<TmModel> list = tmModelMapper.selectByExample(ex);
		return list.get(0);
	}

	/**
	 *
	 * @author lyq 获取车型展示数据
	 * @date 2018年1月26日
	 * @param seriesCode
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getSeriesGroup(String seriesCode, String displacement, String gearboxType) {
		if (CheckUtil.NullOrEmpty(seriesCode)) {
			logger.warn("getSeriesGroup-seriesCode is null");
			throw new ServiceBizException("seriesCode is null");
		}
		List<Map<String, Object>> dtoList = new ArrayList<>();
		List<Map<String, Object>> serList = new ArrayList<>();
		Example ex = new Example(TmModel.class);
		Criteria cr = ex.createCriteria();
		cr.andEqualTo("deleteFlag", "1");
		cr.andEqualTo("seriesCode", seriesCode);
		if (!CheckUtil.NullOrEmpty(displacement)) {
			cr.andEqualTo("displacement", displacement);
		}
		if (!CheckUtil.NullOrEmpty(gearboxType)) {
			cr.andEqualTo("gearboxType", gearboxType);
		}
		List<TmModel> list = tmModelMapper.selectByExample(ex);
		dealModel(dtoList, list);
		if (!CheckUtil.NullOrEmpty(dtoList)) {
			serList = (List<Map<String, Object>>) dtoList.get(0).get("serList");
		}
		return serList;
	}
}
